using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;

using EntLibEC.Cmn;
using EntLibEC.Info.Online;

namespace EntLibEC.Dac.Online
{
	/// <summary>
	/// Summary description for ProductQuestionDac.
	/// </summary>
	public class ProductQuestionDac
	{
		
		public ProductQuestionDac()
		{
			
		}
		public int Insert(ProductQuestionInfo oParam)
		{
			string sql = @"INSERT INTO Ent_Product_Question
                            (
                            ProductSysNo, Email, CreateTime, 
                            CustomerSysNo, NickName, Question, Reply, 
                            ReplyTime, ReplyUserSysNo, Status
                            )
                            VALUES (
                            @ProductSysNo, @Email, @CreateTime, 
                            @CustomerSysNo, @NickName, @Question, @Reply, 
                            @ReplyTime, @ReplyUserSysNo, @Status
                            )";
			
			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramProductSysNo = new SqlParameter("@ProductSysNo", SqlDbType.Int,4);
			SqlParameter paramEmail = new SqlParameter("@Email", SqlDbType.NVarChar,50);
			SqlParameter paramCreateTime = new SqlParameter("@CreateTime", SqlDbType.DateTime);
			SqlParameter paramCustomerSysNo = new SqlParameter("@CustomerSysNo", SqlDbType.Int,4);
			SqlParameter paramNickName = new SqlParameter("@NickName", SqlDbType.NVarChar,50);
			SqlParameter paramQuestion = new SqlParameter("@Question", SqlDbType.NVarChar,1000);
			SqlParameter paramReply = new SqlParameter("@Reply", SqlDbType.NVarChar,500);
			SqlParameter paramReplyTime = new SqlParameter("@ReplyTime", SqlDbType.DateTime);
			SqlParameter paramReplyUserSysNo = new SqlParameter("@ReplyUserSysNo", SqlDbType.Int,4);
			SqlParameter paramStatus = new SqlParameter("@Status", SqlDbType.Int,4);

			if ( oParam.SysNo != AppConst.IntNull)
				paramSysNo.Value = oParam.SysNo;
			else
				paramSysNo.Value = System.DBNull.Value;
			if ( oParam.ProductSysNo != AppConst.IntNull)
				paramProductSysNo.Value = oParam.ProductSysNo;
			else
				paramProductSysNo.Value = System.DBNull.Value;
			if ( oParam.Email != AppConst.StringNull)
				paramEmail.Value = oParam.Email;
			else
				paramEmail.Value = System.DBNull.Value;
			if ( oParam.CreateTime != AppConst.DateTimeNull)
				paramCreateTime.Value = oParam.CreateTime;
			else
				paramCreateTime.Value = System.DBNull.Value;
			if ( oParam.CustomerSysNo != AppConst.IntNull)
				paramCustomerSysNo.Value = oParam.CustomerSysNo;
			else
				paramCustomerSysNo.Value = System.DBNull.Value;
			if ( oParam.NickName != AppConst.StringNull)
				paramNickName.Value = oParam.NickName;
			else
				paramNickName.Value = System.DBNull.Value;
			if ( oParam.Question != AppConst.StringNull)
				paramQuestion.Value = oParam.Question;
			else
				paramQuestion.Value = System.DBNull.Value;
			if ( oParam.Reply != AppConst.StringNull)
				paramReply.Value = oParam.Reply;
			else
				paramReply.Value = System.DBNull.Value;
			if ( oParam.ReplyTime != AppConst.DateTimeNull)
				paramReplyTime.Value = oParam.ReplyTime;
			else
				paramReplyTime.Value = System.DBNull.Value;
			if ( oParam.ReplyUserSysNo != AppConst.IntNull)
				paramReplyUserSysNo.Value = oParam.ReplyUserSysNo;
			else
				paramReplyUserSysNo.Value = System.DBNull.Value;
			if ( oParam.Status != AppConst.IntNull)
				paramStatus.Value = oParam.Status;
			else
				paramStatus.Value = System.DBNull.Value;

			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramProductSysNo);
			cmd.Parameters.Add(paramEmail);
			cmd.Parameters.Add(paramCreateTime);
			cmd.Parameters.Add(paramCustomerSysNo);
			cmd.Parameters.Add(paramNickName);
			cmd.Parameters.Add(paramQuestion);
			cmd.Parameters.Add(paramReply);
			cmd.Parameters.Add(paramReplyTime);
			cmd.Parameters.Add(paramReplyUserSysNo);
			cmd.Parameters.Add(paramStatus);

			return SqlHelper.ExecuteNonQuery(cmd);
		}

		public int Update(Hashtable paramHash)
		{
			StringBuilder sb = new StringBuilder(200);
			sb.Append("UPDATE Ent_Product_Question SET ");

			if ( paramHash != null && paramHash.Count != 0 )
			{
				int index = 0;
				foreach(string key in paramHash.Keys)
				{
					object item = paramHash[key];
					if ( key.ToLower()=="sysno" )
						continue;

					if ( index != 0 )
						sb.Append(",");
					index++;

					
					if (item is int || item is decimal)
					{
						sb.Append(key).Append("=").Append(item.ToString());
					}
					else if (item is string)
					{
						sb.Append(key).Append(" = ").Append(Util.ToSqlString(item.ToString()));
					}
					else if (item is DateTime)
					{
						sb.Append(key).Append(" = cast(").Append( Util.ToSqlString(((DateTime)item).ToString(AppConst.DateFormatLong))).Append(" as DateTime)");
					}
				}
			}

			sb.Append(" WHERE SysNo=").Append(paramHash["SysNo"]);

			return SqlHelper.ExecuteNonQuery(sb.ToString());
		}
	}
}
